<?php

class aoiFrontBookingConfirmAction extends sfAction
{
	public function execute($request)
  {
    
		$step                      = $request->getParameter('step','flight');
    $this->flight_type         = $request->getParameter('flight_type');
    $this->origin_airport      = $request->getParameter('origin_airport');
    $this->destination_airport = $request->getParameter('destination_airport');
    $this->flight_go           = $request->getParameter('flight_go');
    $this->flight_back         = $request->getParameter('flight_back');
    $this->passengers          = $request->getParameter('passengers', array('1'));
    $this->passenger_infos     = $request->getParameter('passenger_infos', array());

    switch($step)
    {
      case 'flight':
        return $this->confirmFlight($request);
        break;
      case 'passengers':
        return $this->confirmPassengers($request);
        break;
      case 'contact':
        return $this->confirmContact($request);
        break;
      case 'all':
        return $this->confirmAll($request);
        break;
      case 'submit':
        return $this->confirmSubmit($request);
        break;
      default:
        return $this->confirmFlight($request);
        break;
    }
	}

  // Main actions

  public function confirmFlight($request)
  {
    $this->listFlights();
    return 'Flight';
  }

  public function confirmPassengers($request)
  {
    return 'Passengers';
  }
  public function confirmContact($request)
  {
    return 'Contact';
  }
  public function confirmAll($request)
  {
    $this->listFlights();
    return 'All';
  }
  public function confirmSubmit($request)
  {
    $this->itineraryReservation = false;

    $this->flights = array();
    if($flightGo = $this->checkFlight($this->flight_go))
    {
      $this->flights []= $flightGo;
    }
    if($this->flight_type == 'go-back' && $flightBack = $this->checkFlight($this->flight_back))
    {
      $this->flights []= $flightBack;
    }

    // create legs if don't exist

    if(count($this->flights) > 0)
    {
      $this->contact_name  = $request->getParameter('last_name');
      $this->contact_email = $request->getParameter('email');

      // create reservation

      $itineraryReservation = new AoiItineraryReservation();
      $itineraryReservation->setContactFirstname($request->getParameter('first_name'));
      $itineraryReservation->setContactLastname($request->getParameter('last_name'));
      $itineraryReservation->setContactEmail($request->getParameter('email'));
      $itineraryReservation->setContactPhone($request->getParameter('phone'));
      $itineraryReservation->setContactAddress($request->getParameter('address'));
      $itineraryReservation->setContactCity($request->getParameter('city'));
      $itineraryReservation->setContactCountry($request->getParameter('country'));

      $bookingAgent = Doctrine_Core::getTable('AoiBookingAgent')->createQuery('a')->fetchOne();
      $itineraryReservation->setAgentId($bookingAgent->getId());

      $itineraryReservation->save();
      $itineraryReservation->generateRefCode();

      // Create passengers
      foreach($this->passenger_infos as $infos)
      {
        $passenger = new AoiPassenger();
        $passenger->setFirstname($infos['first_name']);
        $passenger->setLastname($infos['last_name']);
        $passenger->setAddress($infos['address']);
        $passenger->setCivility($infos['civility']);
        $passenger->setCity($infos['city']);
        $passenger->setCountry($infos['country']);
        $passenger->setCategoryId($infos['category_id']);
        $passenger->setIdType($infos['id_type']);
        $passenger->setIdRef($infos['id_ref']);
        $passenger->setNotes($infos['notes']);
        $passenger->setBirthday($infos['birthday_year'].'-'.$infos['birthday_month'].'-'.$infos['birthday_day'].' 00-00-00');
        $passenger->setReservationId($itineraryReservation->getId());
        $passenger->save();
        $passenger->generateTicketCode();
      }


      $flight_codes = array();

      foreach($this->flights as $flight)
      {
        // generate actual leg
        $leg = $flight->getOrCreateLeg(count($this->passenger_infos));
        $flight_codes []= $leg->getFlightCode();

        // Link reservation and leg
        $itineraryReservationLeg = new AoiItineraryReservationLeg();
        $itineraryReservationLeg->setReservationId($itineraryReservation->getId());
        $itineraryReservationLeg->setLegId($leg->getId());
        $itineraryReservationLeg->save();
      }

      $flight_codes_str = join('-', $flight_codes);
      $itineraryReservation->setFlightCodes($flight_codes_str);
      $itineraryReservation->save();
      $this->itineraryReservation = $itineraryReservation;

    }

    if($this->itineraryReservation)
    {
      $this->getUser()->setFlash('reservation_id', $this->itineraryReservation->getId());
      $this->itineraryReservation->notifyStatusEmail();
      $this->itineraryReservation->save();
    }
    $this->redirect('booking_completed');
  }

  // Common functions

  public function listFlights()
  {
    $this->flights = array();
    if($flightGo = $this->checkFlight($this->flight_go))
    {
      $this->flights []= $flightGo;
    }
    if($this->flight_type == 'go-back' && $flightBack = $this->checkFlight($this->flight_back))
    {
      $this->flights []= $flightBack;
    }
  }

  public function checkFlight($flight_str)
  {
    $parameters = explode('_', $flight_str);
    if(count($parameters) == 2)
    {
      $searchFlight = Doctrine_Core::getTable('AoiFlightSchedule')->getFlightOnDate($parameters[0], $parameters[1]);
      return $searchFlight;
    }
    return false;
  }
}